

package com.maoshi.shop.coupon.multishop.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.maoshi.shop.bean.enums.CouponType;
import com.maoshi.shop.bean.enums.OfflineHandleEventType;
import com.maoshi.shop.bean.model.OfflineHandleEvent;
import com.maoshi.shop.bean.param.OfflineHandleEventAuditParam;
import com.maoshi.shop.common.exception.MaoshiShopBindException;
import com.maoshi.shop.common.util.PageParam;
import com.maoshi.shop.coupon.comment.model.Coupon;
import com.maoshi.shop.coupon.comment.service.CouponService;
import com.maoshi.shop.security.multishop.util.SecurityUtils;
import com.maoshi.shop.service.OfflineHandleEventService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * @author lgh on 2018/12/27.
 */
@RestController("adminCouponController")
@RequestMapping("/admin/coupon")
@AllArgsConstructor
public class CouponController {

    private final CouponService couponService;
    private final OfflineHandleEventService offlineHandleEventService;

    /**
     * 分页获取
     */
    @GetMapping("/page")
    public ResponseEntity<IPage<Coupon>> page(Coupon coupon, PageParam<Coupon> page) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        IPage<Coupon> coupons = couponService.page(page,
                new LambdaQueryWrapper<Coupon>()
                        .like(StrUtil.isNotBlank(coupon.getCouponName()), Coupon::getCouponName, coupon.getCouponName())
                        .eq(coupon.getOverdueStatus() != null, Coupon::getOverdueStatus, coupon.getOverdueStatus())
                        .eq(coupon.getPutonStatus() != null, Coupon::getPutonStatus, coupon.getPutonStatus())
                        .ge(coupon.getStartTime() != null, Coupon::getStartTime, coupon.getStartTime())
                        .le(coupon.getEndTime() != null, Coupon::getEndTime, coupon.getEndTime())
                        .eq(coupon.getCouponType() != null, Coupon::getCouponType, coupon.getCouponType())
                        .eq(Coupon::getShopId, shopId)
                        .orderByDesc(Coupon::getCouponId));
        return ResponseEntity.ok(coupons);
    }

    /**
     * 获取信息
     */
    @GetMapping("/info/{id}")
    public ResponseEntity<Coupon> info(@PathVariable("id") Long id) {
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(id);
        return ResponseEntity.ok(coupon);
    }

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody @Valid Coupon coupon) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        coupon.setShopId(shopId);
        coupon.setSourceStock(coupon.getStocks());
        //判断为领取后生效类型时，开始时间和结束时间为空
        if(Objects.equals(coupon.getValidTimeType(),2)){
            coupon.setEndTime(null);
            coupon.setStartTime(null);
        }
        couponService.insertCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改
     */
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody @Valid Coupon coupon) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Coupon dbCoupon = couponService.getById(coupon.getCouponId());
        if (!Objects.equals(dbCoupon.getShopId(), shopId)) {
            throw new MaoshiShopBindException("没有权限修改该优惠券信息");
        }
        coupon.setShopId(shopId);
        couponService.updateCouponAndCouponProds(coupon);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除
     */
    @DeleteMapping
    public ResponseEntity<Void> delete(@RequestBody Long couponId) {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        Coupon dbCoupon = couponService.getById(couponId);
        if (!Objects.equals(dbCoupon.getShopId(), shopId)) {
            throw new MaoshiShopBindException("没有权限修改该优惠券信息");
        }
        couponService.deleteByCouponId(couponId);
        return ResponseEntity.ok().build();
    }

    /**
     * 通过活动id获取下线信息
     */
    @GetMapping("/getOfflineHandleEventByCouponId/{couponId}")
    public ResponseEntity<OfflineHandleEvent> getOfflineHandleEventByDiscountId(@PathVariable("couponId") Long couponId) {
        OfflineHandleEvent offlineHandleEvent = offlineHandleEventService.getProcessingEventByHandleTypeAndHandleId(OfflineHandleEventType.COUPON.getValue(), couponId);
        return ResponseEntity.ok(offlineHandleEvent);
    }

    /**
     * 违规商品提交审核
     */
    @PostMapping("/auditApply")
    public ResponseEntity<Void> auditApply(@RequestBody OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        Coupon coupon = couponService.getCouponAndCouponProdsByCouponId(offlineHandleEventAuditParam.getHandleId());
        if (coupon == null) {
            throw new MaoshiShopBindException("未找到该活动信息");
        }
        couponService.auditApply(offlineHandleEventAuditParam.getEventId(), offlineHandleEventAuditParam.getHandleId(), offlineHandleEventAuditParam.getReapplyReason());
        // 移除缓存
        couponService.removeCouponAndCouponProdsCache(coupon.getShopId());
        return ResponseEntity.ok().build();
    }
}
